﻿<common:LayoutAwarePage
    x:Name="pageRoot"
    x:Class="WinRTSock.Test.ChatPage"
    DataContext="{Binding DefaultViewModel, RelativeSource={RelativeSource Self}}"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:WinRTSock.Test"
    xmlns:common="using:WinRTSock.Test.Common"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Page.Resources>
        <x:String x:Key="PageTitle">WinRTSock chat</x:String>
        <DataTemplate x:Key="messageItemTemplate">
            <TextBlock Text="{Binding}" Foreground="Black" TextWrapping="Wrap" FontSize="12" Margin="0" Padding="0" FontFamily="Courier New" />
        </DataTemplate>
    </Page.Resources>

    <!--
        This grid acts as a root panel for the page that defines two rows:
        * Row 0 contains the back button and page title
        * Row 1 contains the rest of the page layout
    -->
    <Grid Style="{StaticResource LayoutRootStyle}">
        <Grid.RowDefinitions>
            <RowDefinition Height="140"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="auto" />
        </Grid.ColumnDefinitions>

        <!-- Back button and page title -->
        <Grid Grid.ColumnSpan="2">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Button x:Name="backButton" Click="GoBack" IsEnabled="{Binding Frame.CanGoBack, ElementName=pageRoot}" Style="{StaticResource BackButtonStyle}"/>
            <TextBlock x:Name="pageTitle" Grid.Column="1" Text="{StaticResource PageTitle}" Style="{StaticResource PageHeaderTextStyle}"/>
        </Grid>

        <Grid Grid.Row="1" Grid.Column="1">
            <Grid.RowDefinitions>
                <RowDefinition Height="auto" />
                <RowDefinition Height="auto" />
                <RowDefinition Height="auto" />
            </Grid.RowDefinitions>
            <Grid Grid.Row="0" Width="400" Background="WhiteSmoke">
                <Grid.RowDefinitions>
                    <RowDefinition Height="auto" />
                    <RowDefinition Height="auto" />
                    <RowDefinition Height="auto" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="auto" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Border Grid.Row="0" Grid.Column="0" Background="Gray" Grid.ColumnSpan="2">
                    <TextBlock Text="Connection Config" Margin="5" />
                </Border>
                <TextBlock Text="Host:" Grid.Column="0" Grid.Row="1" Style="{StaticResource FormLabel}" />
                <TextBlock Text="Port:" Grid.Column="0" Grid.Row="2" Style="{StaticResource FormLabel}" />
                <TextBox Text="{Binding HostName, Mode=TwoWay}" Grid.Column="1" Grid.Row="1" BorderBrush="Black" Margin="5"/>
                <TextBox Text="{Binding Port, Mode=TwoWay}" Grid.Column="1" Grid.Row="2" BorderBrush="Black" Margin="5" />
            </Grid>
            <Grid Grid.Row="1" Width="400" Margin="20" Background="WhiteSmoke">
                <Grid.RowDefinitions>
                    <RowDefinition Height="auto" />
                    <RowDefinition Height="auto" />
                    <RowDefinition Height="auto" />
                </Grid.RowDefinitions>
                <Border Grid.Row="0" Background="Gray">
                    <TextBlock Text="Server" Margin="5" HorizontalAlignment="Left" VerticalAlignment="Center" />
                </Border>
                <Grid Grid.Column="0" Grid.Row="1">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="auto" />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="auto" />
                        <RowDefinition Height="auto" />
                        <RowDefinition Height="auto" />
                    </Grid.RowDefinitions>
                    <TextBox Text="{Binding SendFromServer, Mode=TwoWay}" VerticalAlignment="Center" Margin="5" BorderBrush="Black"/>
                    <Button Content="Send" Grid.Column="1" Command="{Binding SendFromServerCommand}" Style="{StaticResource ResourceKey=ButtonStyle}" />
                    <TextBlock Text="Received by server:" Grid.Row="1" Grid.ColumnSpan="2" Style="{StaticResource ResourceKey=FormLabel}" HorizontalAlignment="Left" />
                    <TextBox Text="{Binding ReceivedByServer, Mode=TwoWay}" Grid.Row="2" Grid.ColumnSpan="2" IsReadOnly="True" VerticalAlignment="Center" Margin="5" BorderBrush="Black"/>
                </Grid>
                <Border Grid.Row="2" Background="Gray">
                    <StackPanel>
                        <Button Content="Start Listening" HorizontalAlignment="Right" Command="{Binding StartCommand}" Visibility="{Binding StartButtonVisibility, Mode=TwoWay}" Style="{StaticResource ResourceKey=ButtonStyle}" />
                        <Button Content="Stop Listening" HorizontalAlignment="Right" Command="{Binding StopCommand}" Visibility="{Binding StopButtonVisibility, Mode=TwoWay}" Style="{StaticResource ResourceKey=ButtonStyle}" />
                    </StackPanel>
                </Border>
            </Grid>
            <Grid Grid.Row="2" Width="400" Margin="20" Background="WhiteSmoke">
                <Grid.RowDefinitions>
                    <RowDefinition Height="auto" />
                    <RowDefinition Height="auto" />
                    <RowDefinition Height="auto" />
                </Grid.RowDefinitions>
                <Border Grid.Row="0" Background="Gray">
                    <TextBlock Text="Client" Margin="5" HorizontalAlignment="Left" VerticalAlignment="Center" />
                </Border>
                <Grid Grid.Column="0" Grid.Row="1">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="auto" />
                        <RowDefinition Height="auto" />
                        <RowDefinition Height="auto" />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="auto" />
                    </Grid.ColumnDefinitions>
                    <TextBox Text="{Binding SendFromClient, Mode=TwoWay}" VerticalAlignment="Center" Margin="5" BorderBrush="Black"/>
                    <Button Content="Send" Grid.Column="1" Command="{Binding SendFromClientCommand}" Style="{StaticResource ResourceKey=ButtonStyle}" />
                    <TextBlock Text="Received by client:" Grid.Row="1" Grid.ColumnSpan="2" Style="{StaticResource ResourceKey=FormLabel}" HorizontalAlignment="Left" />
                    <TextBox Text="{Binding ReceivedByClient, Mode=TwoWay}" Grid.Row="2" Grid.ColumnSpan="2" IsReadOnly="True" VerticalAlignment="Center" Margin="5" BorderBrush="Black"/>
                </Grid>
                <Border Grid.Row="2" Background="Gray">
                    <StackPanel>
                        <Button Content="Connect Client" Command="{Binding ConnectCommand}" Visibility="{Binding ConnectButtonVisibility, Mode=TwoWay}" Style="{StaticResource ResourceKey=ButtonStyle}" />
                        <Button Content="Disconnect Client" Command="{Binding DisconnectCommand}" Visibility="{Binding DisconnectButtonVisibility, Mode=TwoWay}" Style="{StaticResource ResourceKey=ButtonStyle}" />
                    </StackPanel>
                </Border>
            </Grid>
        </Grid>


        <ListBox Grid.Row="1" Grid.Column="0"  ItemsSource="{Binding Messages, Mode=TwoWay}" 
                 ItemTemplate="{StaticResource ResourceKey=messageItemTemplate}" Padding="0" Margin="20" ItemContainerStyle="{StaticResource ListBoxItemStyle}" />

        <VisualStateManager.VisualStateGroups>

            <!-- Visual states reflect the application's view state -->
            <VisualStateGroup x:Name="ApplicationViewStates">
                <VisualState x:Name="FullScreenLandscape"/>
                <VisualState x:Name="Filled"/>

                <!-- The entire page respects the narrower 100-pixel margin convention for portrait -->
                <VisualState x:Name="FullScreenPortrait">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="backButton" Storyboard.TargetProperty="Style">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PortraitBackButtonStyle}"/>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>

                <!-- The back button and title have different styles when snapped -->
                <VisualState x:Name="Snapped">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="backButton" Storyboard.TargetProperty="Style">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource SnappedBackButtonStyle}"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="pageTitle" Storyboard.TargetProperty="Style">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource SnappedPageHeaderTextStyle}"/>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
    </Grid>
</common:LayoutAwarePage>
